function [answer age errors]=learn(activationFunc, trainSet, expectedOuts, etha2)
    global etha;
    global epsilon;
    global beta;
    epsilon= 0.001;
    etha= etha2;
    beta=1;
    age = 0;
    [rowsO colsO] = size(expectedOuts);
    [rowsT colsT] = size(trainSet);
    trainSet = horzcat(trainSet, ones(rowsT, 1).*(-1));
    colsT=colsT+1;
    
    W=zeros(colsO,colsT)
    W=setRandomWeigths(W)
    totalError = 1;
    while totalError>epsilon
        randIndex = randperm(rowsT);
        for i=1:rowsT
                %calculo potencial de membrana
                h = W * trainSet(randIndex(i), :)';
                outputs(i) = g(activationFunc, h);
                %guardo la salida esperada para calcular el error cuadratico medio despues
                expectedOutputs(i) = expectedOuts(randIndex(i));
                %calculo el nuevo W
                W = newW(activationFunc, h, outputs(i), trainSet(randIndex(i), :), W, expectedOutputs(i));
        end
        totalError = error(outputs, expectedOutputs)
        age = age + 1
        errors(age)=totalError;
        outputs
        expectedOutputs
    end
    answer=W;
end



function answer=setRandomWeigths(W)
    ceil = 0.5;
    floor = -0.5;
    answer = rand(size(W))*(ceil - floor) + floor;
    %answer=ones(size(W));
end


function newW = newW(activationFunc, h, output, trainPattern, W, expectedOut)
    global etha;
    delta = (expectedOut - output)*gDer(activationFunc, h);
    deltaW = ((trainPattern)*delta)*etha;
    newW = updateWeight(deltaW, W);
end

function answer=updateWeight(deltaW, W)

    answer = W + deltaW;

end

function totalError = error(output, expectedOutput)
    errors = output - expectedOutput;
    totalError = sum(errors.^2)*0.5;
end

function ans=g(activationFunc, x)
    global beta;
    switch(activationFunc)
        case 'step'
            ans = sign(x);
        case 'linear'
            ans = x;
        case 'tanh'
            ans = tanh(beta*x);
        case 'exp'
            ans = 1/(1+exp(-2*beta*x));
    end        
end

function ans=gDer(activationFunc, x)
    global beta;
    switch(activationFunc)
        case 'tanh'
            ans = beta*(1-g(activationFunc, x)^2);
        case 'exp'
            ans = 2*beta*g(activationFunc, x)*(1-g(activationFunc, x));
        otherwise
            ans = 1;
    end    
end